package com.wj.rspmproperty.controller;

import com.wj.rspmproperty.pojo.constant.FreightBasisData;
import com.wj.rspmproperty.pojo.FreightBasis;
import com.wj.rspmproperty.pojo.ResponsePojo;
import com.wj.rspmproperty.service.FreightBasisService;
import com.wj.rspmproperty.uitls.StringUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author WeiJin
 * @version 1.0
 */
@RestController
@RequestMapping("/freightBasis")
@Api(tags = "费率相关API")
public class FreightBasisController {

    @Autowired
    private FreightBasisData freightBasisData;
    @Autowired
    private FreightBasisService freightBasisService;

    /**
     * 查询费用说明
     *
     * @return
     */
    @ApiOperation(value = "查询费用说明")
    @ApiParam
    @PostMapping("/queryAll")
    public ResponsePojo<FreightBasisData> queryAll() {
        return ResponsePojo.success(freightBasisData);
    }


    /**
     * 修改费用说明
     * @param basisPrice 修改后的值 后续会乘100
     * @param fh
     * @param response
     * @return
     */
    @ApiOperation(value = "修改费用说明，可以修改单价、说明或名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "price", value = "单价，两位小数"),
            @ApiImplicitParam(name = "intro", value = "费用介绍"),
            @ApiImplicitParam(name = "basis_name", value = "费用名称"),
            @ApiImplicitParam(name = "freight_basis_id", value = "费用id", dataType = "int")
    })
    @PutMapping("/updateById")
    public ResponsePojo<Integer> update( @RequestParam(value = "basisPrice",required = false) String basisPrice,
                                        @RequestBody @ApiIgnore FreightBasis fh,
                                        HttpServletResponse response) {
        try {
            fh.setPrice(StringUtil.stringPriceToInt(basisPrice));
        } catch (Exception e) {
            try {
                response.sendError(403,e.getMessage());
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            return null;
        }

        int result = freightBasisService.updateById(fh);
        switch (result){
            case 1:
                freightBasisData.flushData();
                return ResponsePojo.success(result);
            case 0:
                return ResponsePojo.fail(0,"该id没有找到对应信息");
            default:
                return ResponsePojo.fail(2,"参数错误，修改失败");
        }


    }
}
